<template>
 <div>
        <div class="login_text"><h1>登录</h1><p>千峰医疗服务后台管理系统</p></div>
          <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="账号" prop="text">
    <el-input type="text" v-model="ruleForm.text" ></el-input>
  </el-form-item>
    <el-form-item label="密码" prop="pass">
    <el-input type="password" v-model="ruleForm.pass" ></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
    <el-button @click="logonFn('ruleForm')">注册</el-button>
  </el-form-item>
</el-form>
 </div>
</template>

<script>
import { LoginFn } from '../api/login';
import eventBus  from '../utils/busEvent'


export default {
 data() {
     
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入账号'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        }  else {
          callback();
        }
      }
      return {
        currentView:'denglu',
        ruleForm: {
          text: '',
          pass: ''
        },

        rules: {
          text: [
            { validator: validatePass, trigger: 'blur' }
          ],
          pass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
         
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {

LoginFn(this.ruleForm).then(res=>{
   if(res.code==200){
      alert('登录成功!');
     this.$router.push('/')
   }else{
      alert('登录失败,请输入正确账号密码!');
   }
})
          
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      logonFn(){ 
          eventBus.$emit('currentView','zhuce')
      }
    }
  }

</script>

<style scoped>
#login .login_text{ 
  text-align: center;
  margin-bottom: 20px;
  margin-left: 70px;

}
#login .login_text p{ 
    margin-top:50px
}
</style>



